/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bumptech.glide.util;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.Matrix;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

/**
 * pixelMap scaling tool class
 */
public class PixelMapScaleUtil {

    /**
     * ScaleMode
     */
    public enum PlaceholderScaleMode {
        CENTER, CLIP_CENTER, STRETCH, ZOOM_START, ZOOM_END, ZOOM_CENTER
    }

    /**
     * Get a new pixelMap according to the given size
     *
     * @param oldPixelMap          raw pixelMap
     * @param newWidth             new pixelMap width
     * @param newHeight            new pixelMap height
     * @param placeholderScaleMode scale mode
     * @return new pixelMap
     */
    public static PixelMap scalePixelMap(PixelMap oldPixelMap, float newWidth, float newHeight,
                                         PlaceholderScaleMode placeholderScaleMode) {
        float oldWidth = oldPixelMap.getImageInfo().size.width;
        float oldHeight = oldPixelMap.getImageInfo().size.height;
        float[] info = getNewPixelMapInfo(oldWidth, oldHeight, newWidth, newHeight, placeholderScaleMode);
        float scalex = info[0];
        float scaley = info[1];
        float left = info[2];
        float top = info[3];

        PixelMap newPixelMap;
        Matrix matrix = new Matrix();
        matrix.scale(scalex, scaley);

        newPixelMap = createPixelMap((int) newWidth, (int) newHeight, oldPixelMap.getImageInfo().pixelFormat);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);

        PixelMapHolder holder = new PixelMapHolder(oldPixelMap);
        Canvas canvas = new Canvas(new Texture(newPixelMap));
        canvas.setMatrix(matrix);
        canvas.drawPixelMapHolder(holder, left / scalex, top / scaley, paint);
        canvas.setTexture(null);

        return newPixelMap;
    }

    private static float[] getNewPixelMapInfo(float oldWidth, float oldHeight, float newWidth, float newHeight,
                                              PlaceholderScaleMode placeholderScaleMode) {
        float[] info = new float[4];
        switch (placeholderScaleMode) {
            case CENTER:
                info[0] = 1.0f;
                info[1] = 1.0f;
                info[2] = (newWidth - oldWidth * info[0]) / 2;
                info[3] = (newHeight - oldHeight * info[1]) / 2;
                break;
            case CLIP_CENTER:
                if (newWidth / oldWidth >= newHeight / oldHeight) {
                    info[0] = newWidth / oldWidth;
                    info[1] = newWidth / oldWidth;
                } else {
                    info[0] = newHeight / oldHeight;
                    info[1] = newHeight / oldHeight;
                }
                info[2] = (newWidth - oldWidth * info[0]) / 2;
                info[3] = (newHeight - oldHeight * info[1]) / 2;
                break;
            case STRETCH:
                info[0] = newWidth / oldWidth;
                info[1] = newHeight / oldHeight;
                info[2] = 0;
                info[3] = 0;
                break;
            case ZOOM_START:
                if (newWidth / oldWidth >= newHeight / oldHeight) {
                    info[0] = newHeight / oldHeight;
                    info[1] = newHeight / oldHeight;
                } else {
                    info[0] = newWidth / oldWidth;
                    info[1] = newWidth / oldWidth;
                }
                info[2] = 0;
                info[3] = 0;
                break;
            case ZOOM_END:
                if (newWidth / oldWidth >= newHeight / oldHeight) {
                    info[0] = newHeight / oldHeight;
                    info[1] = newHeight / oldHeight;
                } else {
                    info[0] = newWidth / oldWidth;
                    info[1] = newWidth / oldWidth;
                }
                info[2] = newWidth - oldWidth * info[0];
                info[3] = newHeight - oldHeight * info[1];
                break;
            default:
                if (newWidth / oldWidth >= newHeight / oldHeight) {
                    info[0] = newHeight / oldHeight;
                    info[1] = newHeight / oldHeight;
                } else {
                    info[0] = newWidth / oldWidth;
                    info[1] = newWidth / oldWidth;
                }
                info[2] = (newWidth - oldWidth * info[0]) / 2;
                info[3] = (newHeight - oldHeight * info[1]) / 2;
                break;
        }
        return info;
    }

    private static PixelMap createPixelMap(int width, int height, PixelFormat config) {
        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
        opts.size = new Size(width, height);
        opts.pixelFormat = config;
        opts.editable = true;
        return PixelMap.create(opts);
    }

}
